package com.tanhua.sso.controller;

import com.tanhua.sso.service.SmsService;
import com.tanhua.sso.vo.ErrorResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author ADMIN
 * @date 2021/8/12 22:42
 */
@RestController
@RequestMapping("user")
@Slf4j
public class SmsController {
    @Autowired
    private SmsService smsService;
    /**
     * 发送验证码
     *
     * @return
     */
    @PostMapping("login")
    public ResponseEntity<Object> sendCheckCode(@RequestBody Map<String,
                Object> param) {
        ErrorResult.ErrorResultBuilder resultBuilder =
                ErrorResult.builder().errCode("000000").errMessage("发送短信验证码失败");
        try {
            String phone = String.valueOf(param.get("phone"));
            Map<String, Object> sendCheckCode =
                    this.smsService.sendCheckCode(phone);
            int code = (Integer) sendCheckCode.get("code");
            if (code == 3) {
                //发送成功
                return ResponseEntity.ok(null);
            }else if(code == 1){
                //发送失败 上一次发送的验证码还未失效
                resultBuilder.errCode("000001").errMessage(sendCheckCode.get("msg").toString());
            }
        } catch (Exception e) {
            log.error("发送短信验证码失败", e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(resultBuilder.
                        build());
    }
    @PostMapping("send")
    public String sendCheckCode(String phone){
        try {
            smsService.senSms(phone);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String("发送成功");
    }

}

